define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Secondkill_agent, GoPage, $ionicSlideBoxDelegate,$timeout) {
        //导航--切换
        $scope.selectIndex = 0;
        $scope.slideChanged = function(index) {
            $scope.selectIndex = index;
        };
        $scope.activeSlide = function(index) {
            $scope.selectIndex = index;
            $ionicSlideBoxDelegate.$getByHandle('main-handle').slide(index);
        };

        //编辑
        $scope.onshow_groupseckill_agent = function(id) {
                GoPage.go_states("groupseckill_agent", {
                    act_id: id
                });
            }
            //统计
        $scope.onshow_statistics_second_agent = function(id) {
            GoPage.go_states("statistics_second_agent", {
                act_id: id
            });
        }

        //加载数据
        $scope.init_data = function() {
            //获取轮播区启用的内容
            Secondkill_agent.getlist(1, 2, 1, function(data) {
                $scope.list = data;
            });
            Secondkill_agent.getlist(1, 2, 3, function(data) {
                $scope.ended = data;
            });
        }
        $scope.init_data();

        //下拉刷新
        $scope.input = new Object();
        $scope.input.nPage = 1;
        $scope.isLoad = false; //可以加载
        //下拉刷新 判断哪个模块进行刷新
        $scope.doRefresh = function() {
            //防止重复加载
            $scope.isLoad = true;
            if($scope.selectIndex == 0) {
                Secondkill_agent.getlist(1, 2, 1, function(data) {
                        console.log(data);
                        if(data != null) {
                            $scope.list = data;

                        } else
                            $scope.data = new Object();
                        $scope.input.nPage = 1;
                    },
                    function() {
                        $scope.$broadcast('scroll.refreshComplete');
                        $timeout(function() {
                            $scope.isLoad = false;
                        }, 1500);
                    });
            } else {
                Secondkill_agent.getlist(1, 2, 3, function(data) {
                        if(data != null) {
                            $scope.ended = data;

                        } else
                            $scope.data = new Object();
                        $scope.input.nPage = 1;
                    },
                    function() {
                        $scope.$broadcast('scroll.refreshComplete');
                        $timeout(function() {
                            $scope.isLoad = false;
                        }, 1500);
                    });
            }

        };
        //加载更多
        $scope.doRefreshMore = function() {
            //防止重复加载
            $scope.isLoad = true;
            if($scope.selectIndex == 0) {
                Secondkill_agent.getlist($scope.input.nPage + 1, 2, 1, function(data) {
                        console.log(data);
                        if(data != null) {
                            if($scope.data.act != undefined) {
                                $scope.list.act = $scope.list.act.concat(data.act);
                                $scope.input.nPage += 1;
                            } else {
                                $scope.list = data;
                            }

                        }
                    },
                    function() {
                        $scope.$broadcast('scroll.infiniteScrollComplete');
                        $timeout(function() {
                            $scope.isLoad = false;
                        }, 1500);

                    });
            } else {
                Secondkill_agent.getlist($scope.input.nPage + 1, 2, 3, function(data) {
                        console.log(data);
                        if(data != null) {
                            if($scope.data.act != undefined) {
                                $scope.ended.act = $scope.ended.act.concat(data.act);
                                $scope.input.nPage += 1;
                            } else {
                                $scope.ended = data;
                            }

                        }
                    },
                    function() {
                        $scope.$broadcast('scroll.infiniteScrollComplete');
                        $timeout(function() {
                            $scope.isLoad = false;
                        }, 1500);

                    });
            }

        };

    }

    controller.$inject = ['$scope', 'Secondkill_agent', 'GoPage', '$ionicSlideBoxDelegate','$timeout'];

    return app.registerController("secondkill_agentctrl",controller);
});